package com.rmt.www.daoImpl;

import java.util.List;

import javax.sql.DataSource;

import org.springframework.dao.EmptyResultDataAccessException;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import com.rmt.www.dao.ProjectDao;
import com.rmt.www.dao.UserDao;
import com.rmt.www.vo.Project;
import com.rmt.www.vo.User;


public class ProjectDaoImpl implements ProjectDao{

	private JdbcTemplate jdbcTemplate;

	public void setDataSource(DataSource dataSource) {
		this.jdbcTemplate = new JdbcTemplate(dataSource);
	}
	

	public void insert(Project project) {
		jdbcTemplate.update("insert into project(title,creatorid,time,description) values(?,?,?,?)", 
				new Object[]{project.getTitle(),project.getCreatorId(),project.getTime(),project.getDescription()}, 
				new int[]{java.sql.Types.VARCHAR,java.sql.Types.INTEGER,java.sql.Types.TIMESTAMP,java.sql.Types.VARCHAR});
	}
	
	public Project getProject(int pid) {
		try {	
			String sql = "select * from project where pid = ?";
			
		    final Object[] params = new Object[] {pid};	
		    
			return  (Project) this.jdbcTemplate.queryForObject(sql,params, new ProjectRowMapper());
		  }catch (final EmptyResultDataAccessException e) {
			  return null;
		  }
		
	}


	@SuppressWarnings("unchecked")
	public List<Project> getList() {

		String sql = "select * from project";
		List<Project> list = this.jdbcTemplate.query(sql, new ProjectRowMapper());
		return list;
	
	}


	public void deleteProject(int pid) {
		String sql="delete from project where pid=?";
		jdbcTemplate.update(sql, new Object[]{pid});
	
	}


	public void updateProject(Project project) {
		jdbcTemplate.update("update project set title=?,creatorid=?,time=?,description=?, isbaselinecreated=? where pid=?", 
				new Object[]{project.getTitle(),project.getCreatorId(),project.getTime(),project.getDescription(), project.isIsbaselinecreated(), project.getPid()}, 
				new int[]{java.sql.Types.VARCHAR,java.sql.Types.INTEGER,java.sql.Types.TIMESTAMP,java.sql.Types.VARCHAR, java.sql.Types.TINYINT, java.sql.Types.INTEGER});
	}

}
